/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clienttest;

import clienttest.common.ConnectManager;
import clienttest.object.ResultReponse;
import clienttest.object.SavingAccountObject;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author lekhanhduy
 */
public class SavingAccount extends javax.swing.JFrame {

    private SavingAccountObject savingAccount = null;
    private WindowAdapter windowAdapter;
    private ConnectManager connectManage;

    /**
     * Creates new form SavingAccount
     */
    public SavingAccount() {
        initComponents();
        this.setLocationRelativeTo(null);
        jScrollHis.setVisible(false);
        jTblHistory.setFillsViewportHeight(true);
        connectManage = new ConnectManager();
        windowAdapter = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent we) {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        };
        this.addWindowListener(windowAdapter);
        jCbxMoney.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"10000000", "20000000", "30000000", "40000000"}));
    }

    public void displayInfoSavingAccount(SavingAccountObject savingAccount) {
        if (savingAccount != null) {
            this.savingAccount = savingAccount;
            jlblInfo.setText("<html>" + this.savingAccount.toString().replace("\n", "<br>") + "</html>");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPnInfoAccount = new javax.swing.JPanel();
        jLblInfoAcount = new javax.swing.JLabel();
        jlblInfo = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jBtnInterest = new javax.swing.JButton();
        jCbxMoney = new javax.swing.JComboBox();
        jBtnDraw = new javax.swing.JButton();
        jLblAction = new javax.swing.JLabel();
        jBtnHistoryTran = new javax.swing.JButton();
        jLblFromDate = new javax.swing.JLabel();
        jDateFromto = new com.toedter.calendar.JDateChooser();
        jLblToDate = new javax.swing.JLabel();
        jDateToDate = new com.toedter.calendar.JDateChooser();
        jScrollHis = new javax.swing.JScrollPane();
        jTblHistory = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMnLogout = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMnHd = new javax.swing.JMenuItem();
        jMnAuthor = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("frmSavingAccount");

        jPnInfoAccount.setBackground(new java.awt.Color(204, 204, 204));
        jPnInfoAccount.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102), 2));

        jLblInfoAcount.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLblInfoAcount.setText("Thông tin tài khoản");

        jlblInfo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlblInfo.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jlblInfo.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout jPnInfoAccountLayout = new javax.swing.GroupLayout(jPnInfoAccount);
        jPnInfoAccount.setLayout(jPnInfoAccountLayout);
        jPnInfoAccountLayout.setHorizontalGroup(
            jPnInfoAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnInfoAccountLayout.createSequentialGroup()
                .addGroup(jPnInfoAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnInfoAccountLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jlblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPnInfoAccountLayout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(jLblInfoAcount)))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        jPnInfoAccountLayout.setVerticalGroup(
            jPnInfoAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnInfoAccountLayout.createSequentialGroup()
                .addComponent(jLblInfoAcount, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlblInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(204, 204, 204));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102), 2));

        jBtnInterest.setText("Hưỡng lãi xuất");
        jBtnInterest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnInterestActionPerformed(evt);
            }
        });

        jCbxMoney.setEditable(true);
        jCbxMoney.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jCbxMoney.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbxMoneyActionPerformed(evt);
            }
        });

        jBtnDraw.setLabel("Rút tiền");
        jBtnDraw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDrawActionPerformed(evt);
            }
        });

        jLblAction.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLblAction.setText("Rút tiền/Hưởng lãi xuất");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jBtnInterest, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jCbxMoney, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jBtnDraw, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(88, 88, 88)
                            .addComponent(jLblAction))))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLblAction)
                .addGap(15, 15, 15)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCbxMoney, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnDraw, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jBtnInterest, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(98, Short.MAX_VALUE))
        );

        jBtnHistoryTran.setLabel("Lịch sử giao dịch");
        jBtnHistoryTran.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnHistoryTranActionPerformed(evt);
            }
        });

        jLblFromDate.setText("Từ ngày : ");

        jLblToDate.setText("Đến ngày : ");

        jScrollHis.setBorder(null);

        jTblHistory.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));
        jTblHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollHis.setViewportView(jTblHistory);

        jMenu1.setText("Tài khoản");

        jMnLogout.setText("Đăng xuất");
        jMnLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMnLogoutActionPerformed(evt);
            }
        });
        jMenu1.add(jMnLogout);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Giới thiệu");

        jMnHd.setText("Hướng dẫn ");
        jMenu2.add(jMnHd);

        jMnAuthor.setText("Tác giả");
        jMnAuthor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMnAuthorActionPerformed(evt);
            }
        });
        jMenu2.add(jMnAuthor);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPnInfoAccount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnHistoryTran)
                        .addGap(18, 18, 18)
                        .addComponent(jLblFromDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDateFromto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(jLblToDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDateToDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollHis, javax.swing.GroupLayout.PREFERRED_SIZE, 720, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPnInfoAccount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jBtnHistoryTran, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLblFromDate, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jDateFromto, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jDateToDate, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLblToDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollHis, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCbxMoneyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCbxMoneyActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCbxMoneyActionPerformed

    private void jMnLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMnLogoutActionPerformed
        // TODO add your handling code here:
        Login login = new Login(new AcountBank());
        login.setVisible(true);
        login.show();
        this.dispose();
    }//GEN-LAST:event_jMnLogoutActionPerformed

    private void jMnAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMnAuthorActionPerformed
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(this,
                "<html>Họ và tên : Lê Khánh Duy <br> SHSV : 20090472 <br> Lớp : Kĩ thuật máy tính và truyền thông 1 K54</html>",
                "Thông tin tác giả!",
                JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jMnAuthorActionPerformed

    private void jBtnDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnDrawActionPerformed
        // TODO add your handling code here:
        if (CheckInputMoney(jCbxMoney)) {
            double input = Double.valueOf(jCbxMoney.getSelectedItem().toString());
            String account = savingAccount.getAccountNumber();
            ResultReponse result = connectManage.withDrawMoneySavingAccount(account, input);
            if (result.isSuccess()) {
                JOptionPane.showMessageDialog(this,
                        result.getNote(),
                        "Rút tiền thành công!",
                        JOptionPane.INFORMATION_MESSAGE);
                displayInfoSavingAccount(connectManage.getInfoSavingAccount(account));
                displayHis();
            } else {
                JOptionPane.showMessageDialog(this,
                        result.getNote(),
                        "Rút tiền lỗi!",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jBtnDrawActionPerformed

    private void jBtnHistoryTranActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnHistoryTranActionPerformed
        // TODO add your handling code here:
        if (savingAccount != null) {
            displayHis();
        }
    }//GEN-LAST:event_jBtnHistoryTranActionPerformed

    private void jBtnInterestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnInterestActionPerformed
        // TODO add your handling code here:
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
        String date = dt.format(new Date());
        ResultReponse reponse = connectManage.caculInterest(savingAccount.getAccountNumber(), date);
        if (reponse.isSuccess()) {
            JOptionPane.showMessageDialog(this,
                    reponse.getNote(),
                    "Hưởng lãi xuất thành công!",
                    JOptionPane.INFORMATION_MESSAGE);
            displayInfoSavingAccount(connectManage.getInfoSavingAccount(savingAccount.getAccountNumber()));
            displayHis();
        } else {
            JOptionPane.showMessageDialog(this,
                    reponse.getNote(),
                    "Hưởng lãi xuất lỗi!",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBtnInterestActionPerformed

    public void displayHis() {
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
        String dateFrom = dt.format(jDateFromto.getDate());
        String dateTo = dt.format(jDateToDate.getDate());
        Object[][] data = connectManage.getAllHistorySavingAccount(savingAccount.getAccountNumber(), dateFrom, dateTo);
        if (data != null) {
            jTblHistory.setModel(new javax.swing.table.DefaultTableModel(
                    data,
                    new String[]{
                        "Loại giao dịch", "Ngày giao dịch", "Giờ giao dịch", "Số tiền"
                    }));
            jScrollHis.setVisible(true);
            DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
            rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
            jTblHistory.getColumnModel().getColumn(3).setCellRenderer(rightRenderer);
            validate();
        }
    }

    public boolean CheckInputMoney(JComboBox jcombobox) {
        boolean result = false;
        if (this.savingAccount != null) {
            double input = 0;
            try {
                input = Double.valueOf(jcombobox.getSelectedItem().toString());
                if (!checkMonney(input)) {
                    JOptionPane.showMessageDialog(this,
                            "Số tiền phải lớn hơn 10.000.000 VND và là bội số của 1.000.000 VND!",
                            "Lỗi đầu vào!",
                            JOptionPane.ERROR_MESSAGE);
                    jcombobox.setSelectedIndex(0);
                } else {
                    result = true;
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this,
                        "Số tiền phải lớn hơn 10.000.000 VND và là bội số của 1.000.000 VND!",
                        "Lỗi đầu vào!",
                        JOptionPane.ERROR_MESSAGE);
                jcombobox.setSelectedIndex(0);
            }
        }
        return result;
    }

    public boolean checkMonney(double money) {
        if (money >= 10000000 && money % 10000000 == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SavingAccount.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SavingAccount.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SavingAccount.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SavingAccount.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SavingAccount().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnDraw;
    private javax.swing.JButton jBtnHistoryTran;
    private javax.swing.JButton jBtnInterest;
    private javax.swing.JComboBox jCbxMoney;
    private com.toedter.calendar.JDateChooser jDateFromto;
    private com.toedter.calendar.JDateChooser jDateToDate;
    private javax.swing.JLabel jLblAction;
    private javax.swing.JLabel jLblFromDate;
    private javax.swing.JLabel jLblInfoAcount;
    private javax.swing.JLabel jLblToDate;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMnAuthor;
    private javax.swing.JMenuItem jMnHd;
    private javax.swing.JMenuItem jMnLogout;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPnInfoAccount;
    private javax.swing.JScrollPane jScrollHis;
    private javax.swing.JTable jTblHistory;
    private javax.swing.JLabel jlblInfo;
    // End of variables declaration//GEN-END:variables
}
